package com.qyhl.shop.shop.coupon.adv;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.shop.R;
import com.qyhl.shop.R2;
import com.qyhl.shop.shop.list.ShopListActivity;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.Md5Builder;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.shop.ShopAdvDetailBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_user.util.LoginUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.SHOP_COUPON_ADV)
public class ShopCouponAdvActivity extends BaseActivity implements ShopCouponAdvContract.ShopCouponAdvView {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.coupon_name)
    TextView couponName;
    @BindView(R2.id.date)
    TextView date;
    @BindView(R2.id.tag)
    TextView tag;
    @BindView(R2.id.coupon_value)
    TextView couponValue;
    @BindView(R2.id.coupon_limit)
    TextView couponLimit;
    @BindView(R2.id.cover)
    RoundedImageView cover;
    @BindView(R2.id.shop_name)
    TextView shopName;
    @BindView(R2.id.address)
    TextView address;
    @BindView(R2.id.contact)
    TextView contact;
    @BindView(R2.id.tag_recycler)
    RecyclerView tagRecycler;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    @Autowired(name = "advId")
    int advId;

    @Autowired(name = "title")
    String titleName;

    @Autowired(name = "isAdv")
    boolean isAdv;


    private CommonAdapter<String> commonAdapter;

    private List<String> mDataList = new ArrayList<>();

    private ShopCouponAdvPresenter mPresenter;

    private ShopAdvDetailBean mBean;


    @Override
    protected int getContentView() {
        return R.layout.shop_activity_coupon_adv;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new ShopCouponAdvPresenter(this);

        title.setText(titleName);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(false);

        tagRecycler.setLayoutManager(new LinearLayoutManager(ShopCouponAdvActivity.this, RecyclerView.HORIZONTAL, false));
        tagRecycler.setAdapter(commonAdapter = new CommonAdapter<String>(ShopCouponAdvActivity.this, R.layout.shop_item_shop_list_tag, mDataList) {

            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                holder.setText(R.id.tag_name, s);
            }
        });

        mPresenter.getDetail(advId,isAdv);
    }

    @Override
    protected void setListener() {
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getDetail(advId,isAdv);
            }
        });

        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDetail(advId,isAdv);
            }
        });
    }


    @OnClick({R2.id.back_btn, R2.id.get_btn, R2.id.shop_layout})
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.back_btn) {
            finish();
        } else if (viewId == R.id.get_btn) {
            LoginUtils.checkLogin(new LoginUtils.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        String userName = CommonUtils.getInstance().getUserName();
                        String sign = Md5Builder.encode(mBean.getShopCouponDto().getId() + "-" + userName + "-" + CommonUtils.getInstance().getSite());
                        showLoading();
                        mPresenter.getCoupon(userName, mBean.getShopCouponDto().getId(), sign);
                    } else {
                        RouterManager.routerLogin(ShopCouponAdvActivity.this, 0);
                        showToast("尚未登录或登录已失效！");
                    }
                }

                @Override
                public void error(String s) {

                }
            });
        } else if (viewId == R.id.shop_layout) {
            ARouter.getInstance()
                    .build(ARouterPathConstant.SHOP_SHOP_DETAIL)
                    .withInt("shopId", mBean.getAdvertiseShopDto().getId())
                    .navigation();

            ActionLogUtils.getInstance().onEvent(ActionConstant.SHOP_FROM_ADV, mBean.getAdvertiseShopDto().getId());
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setDetail(ShopAdvDetailBean bean) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mBean = bean;

        shopName.setText(bean.getAdvertiseShopDto().getName());

        address.setText(bean.getAdvertiseShopDto().getAddress());

        contact.setText(bean.getAdvertiseShopDto().getPhone());

        Glide.with(ShopCouponAdvActivity.this)
                .load(bean.getAdvertiseShopDto().getImageUrl())
                .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                .into(cover);

        mDataList.clear();
        mDataList.addAll(bean.getAdvertiseShopDto().getCouponList());
        commonAdapter.notifyDataSetChanged();

        couponName.setText("仅在" + bean.getShopCouponDto().getShopName() + "店铺使用");

        date.setText("有效期：" + bean.getShopCouponDto().getBeginTime() + " ~ " + bean.getShopCouponDto().getEndTime());

        if (bean.getShopCouponDto().getType() == 1) {
            tag.setVisibility(View.GONE);
            couponValue.setText((bean.getShopCouponDto().getDiscount() == 0 ? 10 : bean.getShopCouponDto().getDiscount()) + "折");
        } else {
            tag.setVisibility(View.VISIBLE);
            couponValue.setText(bean.getShopCouponDto().getReduction() + "");
        }


        if (bean.getShopCouponDto().getThreshold() == 0) {
            couponLimit.setText("无门槛");
        } else {
            couponLimit.setText("满" + bean.getShopCouponDto().getThreshold() + "可用");
        }
    }

    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setErrorText(msg);
        loadMask.setReloadButtonText("点击重试~");
    }

    @Override
    public void getCouponResult(String msg) {
        dismissLoading();
        showToast(msg);
    }

}
